package com.thread.exe;

import java.math.BigInteger;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class BrokenPrimeProducer extends Thread{
	private final BlockingQueue<BigInteger> queue;
	private volatile boolean cancelled  =false;
	
	public BrokenPrimeProducer(BlockingQueue<BigInteger> queue) {
		super();
		this.queue = queue;
	}
	
	@Override
	public void run() {
		try {
			BigInteger p = BigInteger.ONE;
			while(!cancelled) {
				p = p.nextProbablePrime();
				queue.put(p);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public void cancel() {
		cancelled = true;
	}
	
	public static void main(String[] args) {
		BlockingQueue<BigInteger> primes = new LinkedBlockingQueue<>();
		BrokenPrimeProducer producer = new BrokenPrimeProducer(primes);
		producer.start();
		
		try {
			while(needMorePrimes()) {
				consume(primes.take());
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			producer.cancel();
		}
	}

	private static void consume(BigInteger take) {
	}

	private static boolean needMorePrimes() {
		return false;
	}
}
